from searxqt.core import log


## Whether PIL is found on the system or not.
HAVE_PIL = False
try:
    import PIL
    HAVE_PIL = True
    del PIL
except ImportError:
    log.debug("PIL not installed! No image support.")


class _ImagesSettings:
    """! Settings for thumbnails.
    """
    def __init__(self):
        self.__enabled = False
        self.__customProxy = False

        ## Use `Qt.FastTransformation` when set to `True`, it will use
        ## `Qt.SmoothTransformation` when set to `False`.
        ## @see https://doc.qt.io/qt-5/qt.html#TransformationMode-enum
        self.fastTransform = True

        ## Maximum amount of thumbnail download threads.
        self.maxThreads = 3

        ## Maximum thumbnail display width in pixels.
        self.maxWidth = 300

        ## Maximum thumbnail display height in pixels.
        self.maxHeight = 300

    @property
    def supported(self):
        """! Returns `True` when `PIL` is found on the system and thus images
             are supported. `False` otherwise.
        """
        return HAVE_PIL

    @property
    def enabled(self):
        """! Whether downloading/displaying thumbnails is enabled or not."""
        return False if not HAVE_PIL else self.__enabled

    @enabled.setter
    def enabled(self, state):
        """! Enable/Disable downloading/displaying thumbnails."""
        self.__enabled = state

    def serialize(self):
        return {
            'enabled': self.enabled,
            'fastTransform': self.fastTransform,
            'maxThreads': self.maxThreads,
            'maxWidth': self.maxWidth,
            'maxHeight': self.maxHeight
        }

    def deserialize(self, data):
        self.enabled = data.get('enabled', False)
        self.fastTransform = data.get('fastTransform', True)
        self.maxThreads = data.get('maxThreads', 3)
        self.maxWidth = data.get('maxWidth', 300)
        self.maxHeight = data.get('maxHeight', 300)

## Global image settings object.
ImagesSettings = _ImagesSettings()
